﻿using System;
using System.Collections.Generic;
using System.Data.SqlClient;
using System.Linq;
using System.Text;
using TpContracts;
using TpLogger;

namespace TpData.Layer {
    internal class SettingsDataLayer : BaseSqlDataLayer {

        #region Members

        private const string saveSettingStatement = @"setting.SaveSetting";
        private const string loadSettingStatement = @"setting.GetSetting";
        private const string errType = "Err";
        private const string logType = "Log";
        private const string logArea = "TpData.Layer.SettingsDataLayer";
        
        #endregion

        #region Load Save Data

        
        public virtual void SaveSetting<T>(Guid settingID,  string domäne, string settingName, T value) {
            SqlParameter[] parameters = GetParameters(settingID, domäne, settingName, value);
            int ret = ExecuteStoredProcedure(saveSettingStatement, parameters);
            if (ret == 0) {
                //toDo: Log   
                Manager.LogDataManager.SaveLog(Contracts.MyUser.UserID, errType, logArea, "SaveMethode mit 0 Return (" + domäne + "/" + settingName + "/" + value.ToString(), true);
            }
        }

        #endregion

        #region Functions

        protected virtual SqlParameter[] GetParameters<T>(Guid settingID, string domäne, string settingName, T value) {
            SqlParameter[] ps = new SqlParameter[4];

            ps[0] = new SqlParameter("@Domäne", domäne);
            ps[1] = new SqlParameter("@SettingName", settingName);
            ps[2] = new SqlParameter("@SettingValue", value.ToString());
            ps[3] = new SqlParameter("@SettingID", settingID);
            return ps;

        }

        #endregion

    }
}
